{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "## This code create some arbitrary variables and initialize them ###\n",
    "# The goal is to show how to define and initialize variables from scratch.\n",
    "\n",
    "import tensorflow as tf\n",
    "from tensorflow.python.framework import ops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#######################################\n",
    "######## Defining Variables ###########\n",
    "#######################################\n",
    "\n",
    "# Create three variables with some default values.\n",
    "weights = tf.Variable(tf.random_normal([2, 3], stddev=0.1),\n",
    "                      name=\"weights\")\n",
    "biases = tf.Variable(tf.zeros([3]), name=\"biases\")\n",
    "custom_variable = tf.Variable(tf.zeros([3]), name=\"custom\")\n",
    "\n",
    "# Get all the variables' tensors and store them in a list.\n",
    "all_variables_list = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "############################################\n",
    "######## Customized initializer ############\n",
    "############################################\n",
    "\n",
    "## Initialation of some custom variables.\n",
    "## In this part we choose some variables and only initialize them rather than initializing all variables.\n",
    "\n",
    "# \"variable_list_custom\" is the list of variables that we want to initialize.\n",
    "variable_list_custom = [weights, custom_variable]\n",
    "\n",
    "# The initializer\n",
    "init_custom_op = tf.variables_initializer(var_list=variable_list_custom )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "########################################\n",
    "######## Global initializer ############\n",
    "########################################\n",
    "\n",
    "# Method-1\n",
    "# Add an op to initialize the variables.\n",
    "init_all_op = tf.global_variables_initializer()\n",
    "\n",
    "# Method-2\n",
    "init_all_op = tf.variables_initializer(var_list=all_variables_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "##########################################################\n",
    "######## Initialization using other variables ############\n",
    "##########################################################\n",
    "\n",
    "# Create another variable with the same value as 'weights'.\n",
    "WeightsNew = tf.Variable(weights.initialized_value(), name=\"WeightsNew\")\n",
    "\n",
    "# Now, the variable must be initialized.\n",
    "init_WeightsNew_op = tf.variables_initializer(var_list=[WeightsNew])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "######################################\n",
    "####### Running the session ##########\n",
    "######################################\n",
    "with tf.Session() as sess:\n",
    "    # Run the initializer operation.\n",
    "    sess.run(init_all_op)\n",
    "    sess.run(init_custom_op)\n",
    "    sess.run(init_WeightsNew_op)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
